package pub.cleangao.memm.loadbalance;

import pub.cleangao.memm.transport.message.RpcRequest;

import java.util.List;

public abstract class AbstractLoadBalance implements LoadBalance {

    @Override
    public String selectServiceAddress(List<String> serviceUrlList, RpcRequest rpcRequest) {
        // 1. 判空
        if (serviceUrlList == null || serviceUrlList.isEmpty()) {
            return null;
        }
        // 2. 如果只有一个的情况
        if (serviceUrlList.size() == 1) {
            return serviceUrlList.get(0);
        }
        // 3. 使用钩子函数，进行选择
        return doSelect(serviceUrlList, rpcRequest);
    }

    protected abstract String doSelect(List<String> serviceUrlList, RpcRequest rpcRequest);

}
